package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.meterService.bean.MsInfoChangeDetailBean;
import com.koron.css2.meterService.dto.MsInfoChangeDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoChangeDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoChangeMapper;
import com.koron.css2.meterService.mapper.MsInfoTempMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BaseBusinessBranchMapper;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 水表变更查询
 * @author bin
 *
 */
public class WmChgQuery implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(WmChgQuery.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<MsInfoChangeDto> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", MsInfoChangeDto.class);
		try {
			List<MsInfoDto> data = new ArrayList<MsInfoDto>();
			String id = (String) req.getData();
			if(StringUtils.isBlank(id)) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("id不能为空");
				return info;
			}
			MsInfoChangeMapper mapper = factory.getMapper(MsInfoChangeMapper.class);
			MsInfoTempMapper msInfoMapper = factory.getMapper(MsInfoTempMapper.class);
			MsInfoChangeDetailMapper detailMapper = factory.getMapper(MsInfoChangeDetailMapper.class);
			MsInfoChangeDto msInfoChange = mapper.selectById(id);
			List<MsInfoChangeDetailBean> details = detailMapper.selectList(id);
			for(MsInfoChangeDetailBean detail : details) {
				MsInfoDto msInfo = msInfoMapper.selectById(detail.getMsInfoId());
				msInfo.setOperateType(detail.getOperateType());
				data.add(setMsInfo(factory, msInfo,userInfo));
			}
			msInfoChange.setMsInfos(data);
			info.setData(msInfoChange);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}
	public MsInfoDto setMsInfo(SessionFactory factory, MsInfoDto t,UserInfoBean userInfo) {
		Map<String, String> meterStatus = Tools.mapDicByCode(factory, "MMS");
		Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF");
		Map<String, String> commOperators = Tools.mapDicByCode(factory, "TXS");
		Map<String, String> transmittalModel = Tools.mapDicByCode(factory, "TPW");
		Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
		Map<String, String> meterPatterns = Tools.mapDicByCode(factory, "MMP");
		Map<String, String> meterSensors = Tools.mapDicByCode(factory, "MSS"); //水表传感器
		Map<String, String> protocols = Tools.mapDicByCode(factory, "XXGY"); //下行规约
		BaseBusinessBranchMapper businessBranchMapper = factory.getMapper(BaseBusinessBranchMapper.class);
		//MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		//List<MeterFactoryVO> meterFactorys = meterFactoryMapper.selectMeterFactoryList(new MeterFactoryQueryBean());
		//List<BaseMeterModelBean> baseMeterModels = meterModelMapper.selectMeterModelList(new BaseMeterModelQueryBean());
		//List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
	//	List<BaseBusinessBranchBean> businessBranchs = businessBranchMapper.selectBusinessBranchList(new BaseBusinessBranchQueryBean());
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		// 水表口径
		if (StringUtils.isNotBlank(t.getMeterBore())){
			MeterBoreBean meterBoreBean = meterBoreMapper.selectMeterBoreById(t.getMeterBore());
			if (meterBoreBean !=null){
				t.setMeterBoreName(meterBoreBean.getBoreName());
			}
		}
			//传输方式
		if (StringUtils.isNotBlank(t.getTransmittalModel())) {
			t.setTransmittalModelName(transmittalModel.get(t.getTransmittalModel()));
		}
		if (StringUtils.isNotBlank(t.getMeterStatus())) {
			// 水表状态
			t.setMeterStatusName(meterStatus.get(t.getMeterStatus()));
		}
		if (StringUtils.isNotBlank(t.getMeterForm())) {
			// 水表形态
			t.setMeterFormName(meterForms.get(t.getMeterForm()));
		}
		if (StringUtils.isNotBlank(t.getMeterType())) {
			// 水表类型
			t.setMeterTypeName(meterTypes.get(t.getMeterType()));
		}
		if (StringUtils.isNotBlank(t.getCommOperator())) {
			// 通讯商
			t.setCommOperatorName(commOperators.get(t.getCommOperator()));
		}
		if (StringUtils.isNotBlank(t.getMeterPattern())) {
			// 水表型式
			t.setMeterPatternName(meterPatterns.get(t.getMeterPattern()));
		}
		if (StringUtils.isNotBlank(t.getMeterSensor())) {
			// 水表传感器
			t.setMeterSensorName(meterSensors.get(t.getMeterSensor()));
		}
		if(StringUtils.isNotBlank(t.getProtocol())) {
			//下行规约
			t.setProtocolName(protocols.get(t.getProtocol()));
		}
		if(t.getValveControl() != null) {
			if(1 == t.getValveControl())
				t.setValveControlName("是");
			if(0 == t.getValveControl())
				t.setValveControlName("否");
		}
		//水表厂商
        MeterFactoryMapper meterFactoryMapper=factory.getMapper(MeterFactoryMapper.class);
        MeterFactoryBean factoryBean =  meterFactoryMapper.selectMeterFactoryById(t.getMeterFactory());     
		// 水表厂家
		t.setMeterFactoryName(factoryBean==null?t.getMeterFactory():factoryBean.getFactoryName());
		//水表型号
		MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
		MeterModelBean modelBean = mapper.selectMeterModelById(t.getMeterModel());
		t.setMeterModelName(modelBean ==null?t.getMeterModel():modelBean.getModelName());
		if (StringUtils.isNotBlank(t.getDepartBelong())) {
//			// 营业网点
//			Optional<BaseBusinessBranchBean> optional = businessBranchs.stream().filter(o -> Objects.equals(o.getId(), t.getDepartBelong())).findFirst();
//			t.setDepartBelongName(optional.orElse(new BaseBusinessBranchBean()).getBranchName());
			//营业所
			BusinessAbodeMapper Businessmapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
			//获取下拉框
			List<SelectVO> businesslist = Businessmapper.businessAbodeSelect(userInfo.getCurWaterCode());
			//营业所
			for (SelectVO selectVO : businesslist) {
				if(selectVO.getId().equals(t.getDepartBelong())) {
					t.setDepartBelongName(selectVO.getName());
				}
			}
		}
		return t;
	}
}
